<template>
	<view>
		<view class="padding32 flex flex_column perinfo">
			<view class="input">
				<input class="" type="text" placeholder="请输入姓名" v-model="form.name">
				<input class="" type="text" placeholder="请输入身份证号" v-model="form.id_code">
				<input class="" type="number" placeholder="请输入年龄" v-model="form.age">
			</view>
			<view class="checked">
				<text>性别</text>
				<view>
					<label class="radio" style="margin-right: 24px;" @click="form.sex=true">
						<radio class="radios" value="r1" :checked="form.sex" />男
					</label>
					<label class="radio">
						<radio class="radios" value="r2" :checked="!form.sex" @click="form.sex=false" />女
					</label>
				</view>
			</view>
			<view class="checked">
				<text>所在地区</text>
				<view>
					<label class="radio" style="margin-right: 24px;" @click="form.is_city=true">
						<radio class="radios" value="r1" :checked="form.is_city" />城市
					</label>
					<label class="radio">
						<radio class="radios" value="r2" :checked="!form.is_city" @click="form.is_city=false" />农村
					</label>
				</view>
			</view>
			<view class="login-btn">
				<button class="btn-size btn-b" @click="resect()">提交</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		person_change_info
	} from "@/api/login"
	export default {
		data() {
			return {
				checked: true,
				form: {
					name: "",
					id_code: "",
					age: "",
					sex: false,
					is_city: false
				},
			};
		},
		methods: {
			checkCode(val) {
				var p = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
				var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
				var parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
				var code = val.substring(17);
				if (p.test(val)) {
					var sum = 0;
					for (var i = 0; i < 17; i++) {
						sum += val[i] * factor[i];
					}
					if (parity[sum % 11] == code.toUpperCase()) {
						return true;
					}
				}
				return false;
			},
			resect() {
				if (!this.form.name) {
					uni.showToast({
						title: "请输入姓名",
						icon: 'error'
					})
					return
				} else if (!this.form.id_code) {
					uni.showToast({
						title: "请输入身份证号",
						icon: 'error'
					})
					return

				} else if (!this.checkCode(this.form.id_code)) {
					uni.showToast({
						title: "身份证号格式有误",
						icon: 'error'
					})
					return
				} else if (!this.form.age) {
					uni.showToast({
						title: "请输入年龄",
						icon: 'error'
					})
					return
				}
				let obj = {
					name: this.form.name,
					id_code: this.form.id_code,
					age: this.form.age,
					sex: this.form.sex ? 1 : 0,
					is_city: this.form.is_city ? 1 : 0,
				}
				person_change_info(obj).then(res => {
					console.log(res);
					if (res.code == 200) {
						uni.setStorageSync("personal",JSON.stringify(res.data))
						uni.showToast({
							title: "提交成功",
							icon: 'success'
						})
						setTimeout(() => {
							uni.switchTab({
								url: "/pages/index/index"
							})
						}, 500)

					} else {
						uni.showToast({
							title: res.message,
							icon: 'error'
						})
					}
				})

			}
		}
	}
</script>

<style lang="scss" scoped>
	.perinfo {
		.input {
			width: 100%;
			position: relative;

			input {
				border-bottom: 1px solid #eeeff3;
				height: 60px;
			}

			.input-p {
				line-height: 20px;
				position: absolute;
				// width: 120px;
				height: 20px;
				font-size: 12px;
				font-family: 'HarmonyOS Sans SC-Regular, HarmonyOS Sans SC';
				font-weight: 400;
				color: #FF7474;
			}
		}

		.login-btn {
			margin-top: 25px;
			width: 100%;
		}

		.btn-size {
			width: 100%;
			height: 48px;
			margin-bottom: 12px;

		}

		.checked {
			width: 100%;
			display: flex;
			justify-content: space-between;
			align-items: center;
			height: 60px;

		}
	}
</style>
